#!/bin/ash

if [ "$1" != "start" ]; then
  exit 0
fi

# on some sytems, fb0 is not immediatly loaded,
# so, give it a chance by waiting a bit (5 seconds, then fail hard)
count=0
until [ -e /dev/fb0 ]; do
  if [ ${count} -ge 50 ]; then
    exit 1
  else
    sleep 0.1
    let count++
  fi
done

## Global variables ##

fbdevWidth=$(cut -d, -f1 /sys/class/graphics/fb0/virtual_size)
fbdevHeight=$(cut -d, -f2 /sys/class/graphics/fb0/virtual_size)

videoPath=$(sed -n "s/^system.splash.path=//p" /boot/recalbox-backup.conf)
if [ -z "${videoPath}" ] || [ ${videoPath} -eq 0 ] || [ ${videoPath} -lt -1 ]; then
  videoPath="/recalbox/system/resources/splash"
  if [ ${fbdevHeight} -le 240 ]; then
    videoPath="${videoPath}/240p"
  fi
fi

videoLength=$(sed -n "s/^system.splash.length=//p" /boot/recalbox-backup.conf)
if [ -z "${videoLength}" ] || [ ${videoLength} -eq 0 ] || [ ${videoLength} -lt -1 ]; then
  videoLength=20
fi

## Functions ##

# turnOffGPiCaseHDMI
#   takes no argument, power off HDMI port to save energy when running in a GPi case
turnOffGPiCaseHDMI() {
  case=$(sed -n 's/^case=\(.*\)/\1/p' /boot/recalbox-boot.conf)
  if [ "${case}" = "GPiV1:1" ]; then
    recallog "GPi case detected: turning HDMI off"
    tvservice -o
  fi
}

# selectVideo
#   takes no argument and outputs a random video from those available
function selectVideo() {
  IFS= find ${videoPath} -maxdepth 1 -name *.mp4 \
    | while read line; do echo "$RANDOM ${line}"; done \
    | sort -n \
    | head -1 \
    | cut -d' ' -f2
}

# stopPlayerWhenTimeoutReached <playerPID> <timeout>
#   stops player running with PID <playerPID> when <timeout> reached
function stopPlayerWhenTimeoutReached() {
  local playerPID="$1"
  local timeout="$2"
  local count=0

  [ ${timeout} -eq -1 ] && return
  until [ ${count} -ge ${timeout} ]; do
    sleep 1
    let count++
  done
  kill -9 ${playerPID}
}

# playWithMPlayer
# starts playing a video on the framebuffer via mplayer
function playWithMPlayer() {
  local videoFile="$(selectVideo)"
  
  if ! which mplayer > /dev/null; then
    return
  fi
  # init alsa audio
  alsactl init
  # disable framebuffer cursor
  echo -e '\033[?17;0;0c' > /dev/tty1
  # play media in fullscreen
  mp_subs="/recalbox/system/resources/splash/recalboxintro.srt"
  mplayer -vo fbdev2 -fs -zoom -x ${fbdevWidth} -y ${fbdevHeight} -sub "${mp_subs}" "${videoFile}" > /dev/null 2>&1 &
  if [ ${videoLength} -ne -1 ]; then
    stopPlayerWhenTimeoutReached $! ${videoLength}
  fi
}

# playWithFFPlay
#   starts playing a video on the framebuffer via ffplay
function playWithFFPlay() {
  local videoFile="$(selectVideo)"

  if ! which ffplay > /dev/null; then
    return
  fi
  ffplay_opts="-hide_banner -autoexit"
  ffplay_srt="subtitles=/recalbox/system/resources/splash/recalboxintro.srt:force_style='FontName=/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf,Fontsize=10'"
  ffplay ${ffplay_opts} -x ${fbdevWidth} -y ${fbdevHeight} -vf "${ffplay_srt}" "${videoFile}" > /dev/null 2>&1 &
  if [ ${videoLength} -ne -1 ]; then
    stopPlayerWhenTimeoutReached $! ${videoLength}
  fi
}

# playWithOMX
#   starts playing an intro video via OMX player
function playWithOMX() {
  local videoFile="$(selectVideo)"

  if ! which omxplayer.bin > /dev/null; then
    return
  fi
  omx_fnt="--font=/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf"
  omx_opt="--adev both --no-keys --layer=10000 --aspect-mode=fill"
  omx_srt="--no-ghost-box --lines=1 --align=left $omx_fnt --font-size=20 --subtitles=/recalbox/system/resources/splash/recalboxintro.srt"
  /usr/bin/omxplayer.bin ${omx_opt} ${omx_srt} ${videoFile} &
  if [ ${videoLength} -ne -1 ]; then
    stopPlayerWhenTimeoutReached $! ${videoLength}
  fi
}

# showIntroBackground
#   displays our intro background via the framebuffer
showIntroBackground() {
  # fbv will only clear the aera it is drawing to,
  # clear framebuffer in case we don't use the same screen/bg ratio
  dd if=/dev/zero of=/dev/fb0 > /dev/null 2>&1
  fbv -f -i /recalbox/system/resources/splash/logo-version.png
  # launching ES just after fbv can cause some screen corruption
  sleep 1
}

# showInstallBackground
#   displays our install background via the framebuffer
showInstallBackground() {
  fbv -f -i /tmp/.install.png
}

## Main ##

if [ -f /tmp/.install.png ]; then
  showInstallBackground &
elif [ "$(head -c 3 /recalbox/recalbox.arch)" = "rpi" ]; then
  ( turnOffGPiCaseHDMI; playWithOMX; showIntroBackground ) &
elif [ "$(head -c 3 /recalbox/recalbox.arch)" = "x86" ]; then
  ( playWithMPlayer; showIntroBackground ) &
else
  ( playWithFFPlay; showIntroBackground ) &
fi

exit 0
